package com.yanqu.road.server.cmd.cross.tradewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eTradeWarBattleType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.tradewar.UserTradeWarInfo;
import com.yanqu.road.logic.bussiness.player.UserPatronsBussiness;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.player.TradeWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_CROSS_TRADE_WAR_PREPARE_QUERY, desc = "跨服商战匹配查询")
public class CrossTradeWarPrepareQueryCmd extends AbstractCommand {
	private int activityId;
	private int battleType;
	private long myUserId;
	private long myServerId;
	private List<Integer> patronsIdList;
	private long enemyUserId;
	private long enemyServerId;
	private String battleUid;
	private int useItemId;
	private UserTradeWarInfo enemyTradeWarInfo;
	private Map<Integer, UserPatrons> enemyPatronsMap;
	private int fastTeamNum;
	private String myUnionId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossTradeWarProto.CrossTradeWarPrepareReqMsg reqMsg = CrossTradeWarProto.CrossTradeWarPrepareReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		battleType = reqMsg.getBattleType();
		myUserId = reqMsg.getMyUserId();
		myServerId = reqMsg.getMyServerId();
		patronsIdList = new ArrayList<>(reqMsg.getPatronsIdListList());
		enemyUserId = reqMsg.getEnemyUserId();
		enemyServerId = reqMsg.getEnemyServerId();
		battleUid = reqMsg.getBattleUid();
		useItemId = reqMsg.getUseItemId();
		fastTeamNum = reqMsg.getFastTeamNum();
		myUnionId = reqMsg.getMyUnionId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = TradeWarActivityMgr.getOpenActivityInfo(activityId);
		if(null == activityInfo){
            ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_NO_FOUND_BATTLE_FAIL;
			return;
		}
		if(eTradeWarBattleType.RandomBattle.getValue() == battleType){
			enemyTradeWarInfo = TradeWarMgr.findCrossRandomEnemy(activityId, myUserId, myServerId);
		}else if(eTradeWarBattleType.Challenge.getValue() == battleType || eTradeWarBattleType.FightBack.getValue() == battleType ||
				eTradeWarBattleType.Track.getValue() == battleType){
			if(!TradeWarActivityMgr.canCrossTradeWar(activityId, enemyUserId)){
				ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_ENEMY_CAN_NO_BATTLE;
			}
			enemyTradeWarInfo = TradeWarMgr.getUserTradeWarInfo(enemyUserId);
		}else {
			ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_BATTLE_TYPE_ERROR;
			return;
		}
		if(null == enemyTradeWarInfo){
			ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_NO_FOUND_ENEMY;
		}else {
			GamePlayer enemyPlayer = GamePlayerMgr.getOnlinePlayer(enemyTradeWarInfo.getUserId());
			if(null != enemyPlayer){
				enemyPatronsMap = enemyPlayer.getModule(PatronsModule.class).getUserPatronsMap();
			}else {
				enemyPatronsMap = UserPatronsBussiness.getUserPatronsMap(enemyTradeWarInfo.getUserId());
			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		CrossTradeWarProto.CrossTradeWarPrepareRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarPrepareRespMsg.newBuilder();
		respMsg.setActivityId(activityId);
		respMsg.setMyUserId(myUserId);
		respMsg.setMyServerId(myServerId);
		respMsg.setRet(ret);
		respMsg.setBattleType(battleType);
		respMsg.setBattleUid(battleUid);
		respMsg.setFastTeamNum(fastTeamNum);
		if(0 == ret){
			if(null != enemyTradeWarInfo){
				respMsg.addAllPatronsIdList(patronsIdList);
				respMsg.setUseItemId(useItemId);
				UserInfo userInfo = UserMgr.getUserInfo(enemyTradeWarInfo.getUserId());
				respMsg.setEnemyUserId(enemyTradeWarInfo.getUserId());
				respMsg.setEnemyScore(enemyTradeWarInfo.getCrossScore());
				respMsg.setEnemyPatronsCount(userInfo.getPatronsCount());
				for(UserPatrons userPatrons : enemyPatronsMap.values()){
					PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
					if(null != patronsMsg){
						respMsg.addEnemyPatronsList(patronsMsg);
					}
				}
				respMsg.setEnemyPatronsAbility(PatronsMgr.getPatronsListAbility(new ArrayList<>(enemyPatronsMap.values())).toString());
				PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId()));
				respMsg.setEnemyBaseInfo(baseMsg);
			}
		}
        respMsg.setMyUnionId(myUnionId);
		int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, myUserId);
		GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_TRADE_WAR_PREPARE_QUERY_NOTIFY, respMsg, 0, activityId, groupId);
	}

}
